<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%@ include file="../../../../header.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<div class="mod-toolbar-top cf" style="margin-top: 10px;">
		<div class="fr" style="margin-top: 30px;">
			公司： <select id="companyname"></select>
			年份： <select id="years">
					<option value="2018">2018年</option>
					<option value="2019">2019年</option>
					<option value="2020">2020年</option>
					<option value="2021">2021年</option>
					<option value="2022">2022年</option>
				</select>
			月份：<select id="months">
					<option value="1">1月</option>
					<option value="2">2月</option>
					<option value="3">3月</option>
					<option value="4">4月</option>
					<option value="5">5月</option>
					<option value="6">6月</option>
					<option value="7">7月</option>
					<option value="8">8月</option>
					<option value="9">9月</option>
					<option value="10">10月</option>
					<option value="11">11月</option>
					<option value="12" >12月</option>
				</select>
			<a href="javascript:void();" class="ui-btn ui-btn-sp mrb" id="btn-search">搜索</a>
			<a href="javascript:void();" class="ui-btn ui-btn-sp mrb" id="btn-start">初始化</a> 
			<a href="javascript:void(0);" class="ui-btn" id="btn-record">刷新</a> 
			<a class="ui-btn mrb" href="/ownshiroredis/logout">退出</a>
		</div>
	</div>
	<div class="grid-wrap" align="left" style="margin-left: 10px;">
		<table id="grid1"></table>
		<div id="pager2"></div>
	</div>
	<script type="text/javascript">
		$.ajax({
			  type:"POST",
			  url:"company",
			  success:function(data){
				  var content = data;
				  $("#companyname").append(data);
			  }
		  });
		function update() {
			alert("update");
			$.ajax({
				url : "update",
				dataType : "json",
				success : function(data) {
					
				},
				erro : function() {
					alert("爆炸了！");
				}
			});
		}
		function deletes() {
			alert("update");
			$.ajax({
				url : "permission/edit",
				dataType : "json",
				success : function(data) {

				},
				erro : function() {
					alert("爆炸了！");
				}
			});
		}
		function clearGrid() {
			$("#grid").jqGrid('clearGridData');
		}
		//添加图标
		function operationFormat(cellValue, options, rowObject) {
			return '<div class="operating" data-id="' + options.rowId + 
			'"><button  style="background-image: null" class="ui-icon-pencilxxxx" title="月结" data-toggle="modal" data-target="#myModal">月结</button>';
		}
		//开关
		var checkBoxIdGen=0;
		function switchCloseFlag(cellValue,option,rowObject){
			checkBoxIdGen++;
			return ' <input disabled=true class="switch-simple-3" id="'+option.colModel.name+'-'+checkBoxIdGen+'" type="checkbox" '+((cellValue==true)?'checked':'')+'><label for="'+option.colModel.name+'-'+checkBoxIdGen+'" class="green"></label>';
		}
		$(function() {
			$("#grid1").jqGrid(
					{
						url : "kaoqinmonth/listKaoqinMonth",
						datatype : "json",
						height : 400,
						width : '100%',
						shrinkToFit : false,
						autoScroll : false,
						rownumbers : true,
						
						rowNum : 5,
						rowList : [ 5, 10, 15 ], //可供用户选择一页显示多少条
						pager : '#pager2', //表格页脚的占位符(一般是div)的id
						sortname : 'id', //初始化的时候排序的字段
						sortorder : "asc", //排序方式,可选desc,asc
						viewrecords : true,
						pgbuttons : true,
						
						cellEdit : true,
						cellsubmit : "clientArray",
						colNames : [ "操作", "id", "公司", "年", "月", "月结标志", "月结时间", "月结人"],
						colModel : [ {
							name : "operating",
							width : 70,
							formatter : operationFormat,
							align : "center",
							sortable : false
						}, {
							name : "id",
							index : "id",
							key : true,
							editable : false,
							sorttype : "int",
							align : "center"
						},  {
							name : "comopanyid",
							editable : false,
							sorttype : "text",
							align : "center"
						},  {
							name : "year",
							editable : false,
							sorttype : "text",
							align : "center"
						},  {
							name : "month",
							editable : false,
							sorttype : "text",
							align : "center"
						},  {
							name : "mcloseflag",
							formatter : switchCloseFlag,
							editable : false,
							sorttype : "text",
							align : "center"
						},  {
							name : "mclosedate",
							editable : false,
							sorttype : "text",
							align : "center"
						},{
							name : "muserid",
							align : "center",
							editable : false,
							sorttype : "text",
							width : 115
						}],
						loadComplete : function() {
							loadover();
						},
						gridComplete : function() {
						},
						afterInsertRow : function() {
						},
						beforeEditCell : function(rowid, cellname, v, iRow,
								iCol) {
							lastrow = iRow;
							lastcell = iCol;
						}
					});
			initTableRow();
			initEvent();
		})
		//设置每行的高度
		function loadover() {
			var grid = $("#grid1");
			var ids = grid.getDataIDs();
			for (var i = 0; i < ids.length; i++) {
				grid.setRowData(ids[i], false, {
					height : 35
				});
			}
		}
		function initTableRow() {
			for (var i = 1; i <= 4; i++) {
				jQuery("#grid1").jqGrid('addRowData', i, {});
			}
			loadover();
		}
		//添加事件
		function initEvent() {
		 	 $("#btn-start").click(function(a){
		 		var companyname = $("#companyname").val();
		 		var years=$("#years").val();
		 		var months=$("#months").val();
				 $.ajax({
					data:{
						"companyname":companyname,
						"years":years,
						"months":months
					},
					type:"POST",
	            	url:"kaoqinmonth/start",
	            	success:function(data){
                		if(data.status==200){
                			Public.tips({
    							content: "初始化成功！"
    						})
                		}else if(data.status==500){
                			Public.tips({
    							type:1,
    							content: "初始化失败！"
    						})
                		}else if(data.status==400){
                			Public.tips({
    							type:1,
    							content: "这一年所有月份数据已经初始化过了，请再勿初始化！"
    						})
                		}else if(data.status==444){
                			Public.tips({
    							type:1,
    							content: "你没有初始化的权限！"
    						})
                		}else if(data.status==222){
                			Public.tips({
    							type:1,
    							content: "上一年没有全部月结，不能初始化这一年！"
    						})
                		}
                	}
				})
			}),
			$("#btn-search").click(function(a){
				handle.search();
			}),
			$("#grid1").on("click", ".operating .ui-icon-pencilxxxx", function(a) {
				var rowid = $(this).parent().data("id");
				var rowdata=$("#grid1").jqGrid("getRowData",rowid);
				handle.operate(rowid,rowdata);
			}),
			$("#btn-record").click(function(a) {
				$('#grid1').jqGrid('setGridParam',{search:false});  
				$("#grid1").jqGrid().trigger("reloadGrid");
			})
		}
		
		handle = {
				operate: function(rowid, rowdata) {
					this.dialog = $.dialog({
						title: "月结",
						data:{
							rowdata:rowdata,
							rowid:rowid,
							callback:this.callback
						},
						content:"url:kaoqinmonth/closemonth",
						width: 500,
						height: 250,
						max: false,
						min: false,
						cache: false,
						lock: true
					});
				},
				search:function(){
					var grid=$("#grid1");
					this.dialog = $.dialog({
						title: "搜索",
						data:{
							callback:this.callback,
							grid:grid
						},
						content:"url:kaoqinmonth/search",
						width : 500,
						height : 250,
						max : false,
						min : false,
						cache : false,
						lock : true
					});
				},
				callback: function(data) {
					if(data!=undefined){
						$('#grid1').jqGrid('setGridParam',{search:true});  
						$("#grid1").jqGrid("setGridParam",{postData:data}).jqGrid("setGridParam",{page:1}).trigger("reloadGrid");
					}else{
						$("#grid1").jqGrid().trigger("reloadGrid");
					}
				}
			};
	</script>
</body>
</html>